package com.seatiger.structure.stack;


import java.util.Stack;

/**
 * 栈的实践应用问题： 括号匹配
 * https://leetcode.cn/problems/valid-parentheses/description/
 * <p>
 * Given a string s containing just the characters '(', ')', '{', '}', '[' and ']',
 * determine if the input string is valid.
 */
public class ValidParentheses {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        char[] chars = s.toCharArray();
        for (char c : chars) {
            if ('(' == c || '{' == c || '[' == c) {
                stack.push(c);
            } else if (stack.isEmpty()) {
                return false;
            } else if (')' == c && stack.pop() == '(') {
            } else if ('}' == c && stack.pop() == '{') {
            } else if (']' == c && stack.pop() == '[') {
            } else {
                return false;
            }
        }
        return stack.isEmpty();
    }
}
